package com.polimi.meteocal.security;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.ScheduleEvent;

import com.polimi.meteocal.business.control.EventManager;
import com.polimi.meteocal.business.control.InviteManager;
import com.polimi.meteocal.business.entity.Event;
import com.polimi.meteocal.business.util.PersonalStringTokenizer;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@ManagedBean
@SessionScoped
public class EventBean implements Serializable {
/*
 * This bean carries out the creation of a new event. The method storeEvent() generates 
 * the new event and stores it, calling the EventManager.
 */
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	EventManager em;
	@EJB
	InviteManager im;
	
	//Event to create
	private Date date;
	private Date duration;
	private String name;
	private String place;
	private boolean outdoor;
	private boolean publicVisibility;
	private String description;
	private String invites;
	private String organizers;
	
	
	

	public EventBean() {
		// TODO Auto-generated constructor stub
		
		
	}
	
	public String storeEvent(){
		
		
		
		//Creation of usersToInvite
		PersonalStringTokenizer pSt= new PersonalStringTokenizer();
		List<String> usersToInvite= pSt.tokenizeString(this.invites);
		
		//Creation of eventOrganizers
		List<String> eventOrganizers= pSt.tokenizeString(this.organizers);
		
		//Creation of event
		Event event= new Event(); 
		event.setDatetimebegin(date);
		event.setDuration(duration);
		event.setName(name);
		event.setPlace(place);
		event.setOutdoor(outdoor);
		event.setPublicvisibility(publicVisibility);
		event.setDescription(description);
		
		//Save event with organizers and invites
		em.saveEvent(event, eventOrganizers);
		im.saveInvites(usersToInvite, event);
		 
		return "calendarPage";
		
	}
	

	//Getters and Setters

	public Date getDate() {
		return date;
	}


	public void setDate(Date date) {
		this.date = date;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public boolean isOutdoor() {
		return outdoor;
	}


	public void setOutdoor(boolean outdoor) {
		this.outdoor = outdoor;
	}


	public boolean isPublicVisibility() {
		return publicVisibility;
	}


	public void setPublicVisibility(boolean publicVisibility) {
		this.publicVisibility = publicVisibility;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public String getPlace() {
		return place;
	}


	public void setPlace(String place) {
		this.place = place;
	}
	
	
	public String getInvites() {
		return invites;
	}


	public void setInvites(String invites) {
		this.invites = invites;
	}


	public String getUsersToInvite() {
		return invites;
	}


	public void setUsersToInvite(String usersToInvite) {
		this.invites = usersToInvite;
	}


	public String getOrganizers() {
		return organizers;
	}


	public void setOrganizers(String organizers) {
		this.organizers = organizers;
	}


	public Date getDuration() {
		return duration;
	}


	public void setDuration(Date duration) {
		this.duration = duration;
	}

	

}
